package cn.xiongxf.org.controller;

import cn.xiongxf.basic.exception.BusinessException;
import cn.xiongxf.basic.util.AjaxResult;
import cn.xiongxf.basic.util.ExcelUtils;
import cn.xiongxf.basic.util.PageList;
import cn.xiongxf.org.domain.Shop;
import cn.xiongxf.org.domain.ShopAuditLog;
import cn.xiongxf.org.domain.ShopDto;
import cn.xiongxf.org.query.ShopQuery;
import cn.xiongxf.org.service.ShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/shop")
@Api(value = "店铺API",description = "店铺的CRUD操作") // 描述Controller
public class ShopController {
    @Autowired
    private ShopService shopService;


    /**
     * 接口：查询所有
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有") // 描述方法
    public List<Shop> findAll(){
        return shopService.findAll();
    }

    /**
     * 接口：查询单个
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个")
    public Shop findById(@PathVariable Long id){
        return shopService.findById(id);
    }

    /**
     * 接口：分页查询
     * @param shopQuery
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询")
    public PageList<Shop> queryPage(@RequestBody ShopQuery shopQuery){
        return shopService.queryPageList(shopQuery);
    }

    /**
     * 接口：删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public AjaxResult del(@PathVariable Long id){
        try {
            shopService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 接口：批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除")
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try{
            shopService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 接口：新增/修改
     * @param shop
     * @return
     */
    @PutMapping
    @ApiOperation(value = "新增/修改")
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try{
            if (shop.getId() == null){
                shopService.add(shop);
            }else {
                shopService.update(shop);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    @PostMapping("/settlement")
    @ApiOperation(value = "店铺入驻")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return new AjaxResult();
        }catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统异常，请稍后重试！！！");
        }
    }

    /**
     * 接口：店铺审核通过
     * @param log
     * @return
     */
    @PostMapping("/audit/pass")
    @ApiOperation(value = "店铺审核通过")
    public AjaxResult auditPass(@RequestBody ShopAuditLog log){
        try {
            shopService.auditPass(log);
            return new AjaxResult();
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"审核失败！");
        }
    }

    /**
     * 接口：店铺审核驳回
     * @param log
     * @return
     */
    @PostMapping("/audit/reject")
    @ApiOperation(value = "店铺审核驳回")
    public AjaxResult auditReject(@RequestBody ShopAuditLog log){
        try {
            shopService.auditReject(log);
            return new AjaxResult();
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"驳回失败！");
        }
    }

    /**
     * 接口：导出Excel表
     * @param response
     */
    @GetMapping("/export")
    @ApiOperation(value = "导出Excel表")
    public void export(HttpServletResponse response){
        try {
            // controller可写少量业务
            List<Shop> shops = shopService.findAll();
            ExcelUtils.exportExcel(shops, null, "店铺信息", Shop.class,"shop.xls",response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 接口：导入Excel表
     * @param file
     */
    @GetMapping("/importExcel")
    @ApiOperation(value = "导入Excel表")
    public void importExcel(@RequestPart("file") MultipartFile file){
        List<Shop> shops = ExcelUtils.importExcel(file, 0, 1, Shop.class);
        System.out.println("一共上传了【"+shops.size()+"】张图片");
        for(Shop shop:shops){
            System.out.println(shop);
        }
        // 保存到数据库中（动态sql，批量插入）
        shopService.patchAdd(shops);
    }

    /**
     * 接口：点击邮件激活
     * @param id
     */
    @GetMapping("active/{id}")
    @ApiOperation(value = "点击邮件激活")
    public String active(@PathVariable Long id){
        shopService.active(id);
        return "激活成功!";
    }

    /**
     * 接口：echarts报表
     * @return
     */
    @GetMapping("/echarts")
    @ApiOperation(value = "echarts报表")
    public List<ShopDto> echarts(){
        List<ShopDto> shopDtos = shopService.getCountByState();
        return shopDtos;
    }

}
